//
//  PPHomeDashboardView.m
//  pickpic
//
//  Created by Jinxiao on 9/6/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPHomeDashboardView.h"

#import "PPHomeDashboardViewBadgeCell.h"

#import "PPProgressiveLogoView.h"

#import "PPUnreadMessageManager.h"

#define LogoHeaderHeight    110

@interface PPHomeDashboardView () <UIScrollViewDelegate>
@property (readwrite, nonatomic, strong) PPProgressiveLogoView *headerView;
@property (readwrite, nonatomic, strong) UIView *footerView;
@property (readwrite, nonatomic, strong) UIView *contentView;
@property (readwrite, nonatomic, strong) PPAbstractScrollView *scrollView;
@property (readwrite, nonatomic, strong) NSArray <PPHomeDashboardViewBadgeCell *> *cells;
@end

@implementation PPHomeDashboardView

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)init {
    self = [super init];

    self.backgroundColor = [UIColor whiteColor];
    self.clipsToBounds = NO;

    _scrollView = [[PPAbstractScrollView alloc] init];
    _scrollView.delegate = self;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.clipsToBounds = NO;
    _scrollView.scrollsToTop = NO;
    _scrollView.alwaysBounceVertical = YES;
    [self addSubview:_scrollView];

    _contentView = [[UIView alloc] init];
    _contentView.backgroundColor = [UIColor blackColor];
    [_scrollView addSubview:_contentView];

    _headerView = [[PPProgressiveLogoView alloc] init];
    _headerView.logoColor = [UIColor colorWithRGBHex:0x00];
    _headerView.clipsToBounds = YES;
    _headerView.backgroundColor = [UIColor clearColor];
    [_scrollView addSubview:_headerView];

    _footerView = [[UIView alloc] init];
    _footerView.backgroundColor = [UIColor blackColor];
    [_contentView addSubview:_footerView];

    PPMainViewBadgeType types[] = {
        PPMainViewBadgeTypeHome,
        PPMainViewBadgeTypeFriend,
        PPMainViewBadgeTypeMiao,
        PPMainViewBadgeTypeFound,
        PPMainViewBadgeTypeFavour,
        PPMainViewBadgeTypeContact,
//        PPMainViewBadgeTypeSetting
    };

    NSMutableArray *cells = [NSMutableArray array];

    for(NSInteger index = 0; index < sizeof(types)/sizeof(PPMainViewBadgeType); index++) {
        PPMainViewBadgeType type = types[index];

        PPHomeDashboardViewBadgeCell *cell = [[PPHomeDashboardViewBadgeCell alloc] init];
        cell.type = type;
        [_contentView addSubview:cell];

        __weak typeof(self)weakSelf = self;
        [cell setActionBlock:^(UIControl *control) {
            [weakSelf.delegate dashboardView:weakSelf didSelectBadgeCell:(PPHomeDashboardViewBadgeCell *)control];
        } forControlEvents:UIControlEventTouchUpInside];

        [cells addObject:cell];
    }

    _cells = cells;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveUnreadMessageSummaryDidChangeNotification) name:PPUnreadMessageSummaryDidChangeNotification object:nil];

    return self;
}

- (void)didMoveToSuperview {
    [super didMoveToSuperview];

    [self updateUnreadBadges];
}

- (void)didReceiveUnreadMessageSummaryDidChangeNotification {
    [self updateUnreadBadges];
}

- (void)updateUnreadBadges {
    [_cells enumerateObjectsUsingBlock:^(PPHomeDashboardViewBadgeCell *_Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {

        PPUnreadMessageSummaryObject *summary = nil;

        switch(obj.type) {

            case PPMainViewBadgeTypeHome: {
                summary = [[PPUnreadMessageManager sharedInstance] summaryForType:PPUnreadMessageSummaryTypeMyNotify];
                break;
            }

            case PPMainViewBadgeTypeFriend: {
                summary = [[PPUnreadMessageManager sharedInstance] summaryForType:PPUnreadMessageSummaryTypeFriend];
                break;
            }

            case PPMainViewBadgeTypeMiao: {
                summary = [[PPUnreadMessageManager sharedInstance] summaryForType:PPUnreadMessageSummaryTypeMiao];
                break;
            }

            case PPMainViewBadgeTypeFound: {
                summary = [[PPUnreadMessageManager sharedInstance] summaryForType:PPUnreadMessageSummaryTypeFound];
                break;
            }

            case PPMainViewBadgeTypeFavour: {
                summary = [[PPUnreadMessageManager sharedInstance] summaryForType:PPUnreadMessageSummaryTypeFavorite];
                break;
            }

            default: break;
        }

        obj.summary = summary;
    }];
}

- (void)layoutSubviews {
    [super layoutSubviews];

    _scrollView.frame = self.bounds;

    _headerView.frame = CGRectMake(0, 0, _scrollView.width, _scrollView.height);

    _scrollView.contentInset = UIEdgeInsetsMake(-_headerView.height, 0, 0, 0);

    _contentView.frame = CGRectMake(0, _headerView.height, _scrollView.width, _scrollView.height);

    _footerView.frame = CGRectMake(0, _contentView.height, _contentView.width, 1000);

    CGFloat cellWidth = _scrollView.width/3;
    CGFloat cellHeight = 110;

    for(NSInteger index = 0; index < _cells.count; index++) {
        NSInteger column = index%3;
        NSInteger row = index/3.0;

        _cells[index].frame = CGRectMake(column*cellWidth, 38 + row*(cellHeight + 38), cellWidth, cellHeight);
    }
}

- (void)dismissWithDuration:(NSTimeInterval)duration animated:(BOOL)animated completion:(void (^)(void))completion {
    UIView *container = [_headerView valueForKey:@"container"];

    CGFloat offset = -_scrollView.contentOffset.y + _headerView.height;

    if(animated) {
        [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState|(4 << 16) animations:^{
            _scrollView.top = self.height;
            _headerView.alpha = 0;
            container.top = offset - (offset + container.height)/2;
        } completion:^(BOOL finished) {
            !completion ? : completion();
        }];
    } else {
        _scrollView.top = self.height;
        _headerView.alpha = 0;
        container.top = offset - (offset + container.height)/2;
        !completion ? : completion();
    }
}

- (void)reset {
    _scrollView.top = 0;

    _headerView.alpha = 1;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat minimumOffset = 0;
    CGFloat maximumOffset = 110;

    CGFloat offset = -scrollView.contentOffset.y + _headerView.height;

    CGFloat progress = (offset - minimumOffset)/(maximumOffset - minimumOffset);

    _headerView.progress = progress;

    _headerView.offset = offset;
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    if(_headerView.progress >= 1) {
        [_delegate dashboardViewDidTriggerToggling:self];
    }
}

- (void)triggerDismiss {
    [_delegate dashboardViewDidTriggerToggling:self];
}

@end
